import request from "@/utils/request";
import {serverIp} from "../../public/config.js";
const API_BASE_URL = `http://${serverIp}:8081/jdyfy`;

/**
 *
 * 用户API请求
 *
 **/
// 登录
export const login = (data) => {
    return request.post(`${API_BASE_URL}/login`, data).then(res => {
        return res.data;
        });
}



// 退出
export const userLogout = (params) =>
    request({
        url: '/employee/logout',
        method: 'post',
        params,
    });

// 分页查询员工列表
export const getUserPage = (pageNum, pageSize, roleId, department) =>{
    return request.get(`${API_BASE_URL}/userPage`,{
        params: {
            pageNum,
            pageSize,
            roleId,
            department
        }
    }).then(res => {
            return res.data;
        })
};


// 根据id查询用户
export const getUserById = (id) => {
    return request.get(`${API_BASE_URL}/user/find/${id}`)
    .then(res => {
        return res.data;
})
};

// 根据姓名查询用户
export const getUserByName = (name) => {
    return request.get(`${API_BASE_URL}/user/findByName`, {params: {name}})
        .then(res => {
            return res.data;
        })
};

// 根据用户名查询用户
export const getUserByUsername = (username) => {
    return request.get(`${API_BASE_URL}/user/findByUsername`, {params: {username}})
        .then(res => {
            return res.data;
        })
};

// 根据角色查询用户
export const getUserByRoleId = (roleId) => {
    return request.get(`${API_BASE_URL}/user/findByRoleId`, {params: {roleId}})
        .then(res => {
            return res.data;
        })
};

// 新增或修改---添加员工
export const addUser = (form) => {
    return request.post(`${API_BASE_URL}/user/save`, form)
        .then(res => {
            return res.data;
        }).catch(err => {
            if(err.response.status === 400 && err.response.data === "用户名已存在，请选择其他用户名"){
                alert('用户已存在，请重新填写注册账号');
            }
        })
};

// 修改密码
export const savePassword = (form) => {
    return request.post(`${API_BASE_URL}/user/savePassword`, form).then(res => {
        return res.data;
    })
};

//删除员工
export const deleteuser = (id) =>{
    return request.delete(`${API_BASE_URL}/user/delete`, {data: id})
        .then(res => {
            return res;
        })
};
// 修改---添加员工
export const editUser = (params) => {
    return request({
        url: '/employee',
        method: 'put',
        data: { ...params },
    });
};

// 修改页面反查详情接口
export const queryEmployeeById = (id) => {
    return request({
        url: `/employee/${id}`,
        method: 'get',
    });
};

// 统计各角色的用户数量
export const getUserCountByRole = () => {
    return request.get(`${API_BASE_URL}/user/userCountByRole`)
        .then(res => {
            return res.data;
        })
};

